//
//  FQDTabbedPageView.m
//  FakeQDaily
//
//  Created by mewxiv on 9/20/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDTabbedPageView.h"
#import "FQDSegmentedControl.h"

@interface FQDTabbedPageView () <UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong, readwrite) NSMutableArray *titles;
@property (nonatomic, strong, readwrite) NSMutableArray *pages;
@end

@implementation FQDTabbedPageView

#pragma mark - View life cycle
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _titles = [NSMutableArray array];
        _pages = [NSMutableArray array];
        
        [self prepareUI];
    }
    return self;
}

- (instancetype)initWithPages:(NSArray *)pages titles:(NSArray *)titles
{
    self = [self initWithFrame:CGRectZero];
    if (self) {
        [self setPages:pages withTitles:titles];
    }
    return self;
}

- (void)prepareUI
{
    _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.bounces = NO;
    _scrollView.delegate = self;
    [self addSubview:_scrollView];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.alignment = NSTextAlignmentCenter;
    NSDictionary *titleAttributes = @{
        NSFontAttributeName : FQDRegularFontWithSize(14.0f),
        NSForegroundColorAttributeName : RGB(136, 136, 136),
        NSParagraphStyleAttributeName : paragraphStyle,
    };
    NSDictionary *selectedTitleAttributes = @{
        NSFontAttributeName : FQDRegularFontWithSize(14.0f),
        NSForegroundColorAttributeName : [UIColor blackColor],
        NSParagraphStyleAttributeName : paragraphStyle,
    };
    _segmentedControl = [[FQDSegmentedControl alloc] init];
    _segmentedControl.selectIndicatorColor = FQDHighlightColor;
    _segmentedControl.selectLineIndicatorHeight = 2.0f;
    _segmentedControl.sectionTitleAttributes = titleAttributes;
    _segmentedControl.selectedSectionTitleAttributes = selectedTitleAttributes;
    [self addSubview:_segmentedControl];
    
    WeakSelf;
    [_segmentedControl setSelectedIndexChangedBlock:^(NSInteger idx) {
        CGFloat pageWidth = weakSelf.scrollView.frame.size.width;
        [weakSelf.scrollView setContentOffset:CGPointMake(idx * pageWidth, 0)
                                     animated:YES];
    }];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGSize viewSize = self.bounds.size;
    CGFloat scHeight = 50.0f;
    _segmentedControl.frame = CGRectMake(0, 0, viewSize.width, scHeight);
    
    self.scrollView.frame = CGRectMake(0, scHeight, viewSize.width, viewSize.height - scHeight);
    [self layoutScrollContents];
}

- (void)layoutScrollContents
{
    CGFloat pageWidth = self.scrollView.frame.size.width;
    CGFloat pageHeight = self.scrollView.frame.size.height;
    [_pages enumerateObjectsUsingBlock:^(UIView * _Nonnull page, NSUInteger idx, BOOL * _Nonnull stop) {
        page.frame = CGRectMake(idx * pageWidth, 0, pageWidth, pageHeight);
    }];
    self.scrollView.contentSize = CGSizeMake([_pages count] * pageWidth, pageHeight);
}


#pragma mark - Getters and Setters
- (NSArray *)titles
{
    return [_titles copy];
}

- (NSArray *)pages
{
    return [_pages copy];
}

- (void)setPages:(NSArray *)pages withTitles:(NSArray *)titles
{
    _titles = [titles mutableCopy];
    _pages = [pages mutableCopy];
    
    for (UIView *subview in self.scrollView.subviews) {
        [subview removeFromSuperview];
    }
    for (UIView * page in pages) {
        [self.scrollView addSubview:page];
    }
    
    [self.segmentedControl setSectionTitles:self.titles];
    
    [self setNeedsLayout];
}

- (void)addPage:(UIView *)page withTitle:(NSString *)title
{
    [_titles addObject:title];
    [_pages addObject:page];
    
    [self.scrollView addSubview:page];
    [self.segmentedControl setSectionTitles:self.titles];
    
    [self setNeedsLayout];
}

#pragma mark - ScrollView Delegate Methods
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat offset = scrollView.contentOffset.x;
    CGFloat pageWidth = scrollView.frame.size.width;
    NSInteger idx = (offset / pageWidth) + 0.5;
    [self.segmentedControl setSelectedSegmentIndex:idx];
}

@end
